package com.lwww.infinitechat.authenticationservice.service.impl;

import com.lwww.infinitechat.authenticationservice.constants.user.RegisterConstant;
import com.lwww.infinitechat.authenticationservice.service.CommonService;
import com.lwww.infinitechat.authenticationservice.utils.RandomNumUtil;
import com.lwww.infinitechat.authenticationservice.utils.SendMailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    private StringRedisTemplate redisTemplate;

//    /**
//     * 发送验证码
//     * @param request
//     * @return
//     */
//    public CommonResponse sendCode(CommonRequest request) throws Exception {
//        String phone = request.getPhone();
//        //生成验证码
//        String code = RandomNumUtil.getRandomNum();
//        //存进redis
//        redisTemplate.opsForValue().set(RegisterConstant.REGISTER_CODE+phone,code,5, TimeUnit.MINUTES);
//        //发送短信
//        return new CommonResponse().setPhone(phone);
//    }


    public void sendMail(String targetMail,String phone) {
        String code = new RandomNumUtil().getRandomNum();
        new SendMailUtil().sendEmailCode(targetMail, code);
        redisTemplate.opsForValue().set(RegisterConstant.REGISTER_CODE+phone,code,5, TimeUnit.MINUTES);
    }

}
